package com.oa.ylms.apps.business.notice.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.notice.INoticeInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.notice.INoticeInfoDao;
import com.oa.ylms.apps.dao.notice.INoticeSectionDao;
import com.oa.ylms.apps.dao.notice.INoticeStaffDao;
import com.oa.ylms.apps.entity.notice.NoticeInfoValue;
import com.oa.ylms.apps.entity.notice.NoticeSectionValue;
import com.oa.ylms.apps.entity.notice.NoticeStaffValue;

public class NoticeInfoServiceImpl implements INoticeInfoService {
	private INoticeInfoDao infoDao;
	private INoticeSectionDao sectionDao;
	private INoticeStaffDao staffDao;
	private NoticeInfoValue noticeInfo;
	private NoticeSectionValue noticeSection;
	private NoticeStaffValue noticeStaff;
	private IStaffInfoService staffInfoService;
	private ISectionInfoService sectionInfoService;
	private List<NoticeStaffValue> listNoticeStaff;
	private List<NoticeSectionValue> listNoticeSection;

	public int addNotice(int staffId, String title, String place,
			Date startTime, Date endTime, String content, String postScript,
			String staffIds, String sectionIds) throws Exception {
		noticeInfo.setStaffInfo(staffInfoService.queryByStaffId(staffId));
		noticeInfo.setTitle(title);
		noticeInfo.setPlace(place);
		noticeInfo.setStartTime(startTime);
		noticeInfo.setEndTime(endTime);
		noticeInfo.setContent(content);
		noticeInfo.setPostScript(postScript);
		noticeInfo.setPublishTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		noticeInfo.setStates(IDEDStates.ENABLE);
		Integer noticeId = infoDao.addNoticeInfo(noticeInfo);
		if (staffIds.length() != 0) {
			staffIds = staffIds.substring(0, staffIds.length() - 1);
			String[] arrayStaffId = staffIds.split(",");
			for (int i = 0; i < arrayStaffId.length; i++) {
				Integer newStaffId = Integer.parseInt(arrayStaffId[i]);
				noticeStaff.setNoticeInfo(queryNoticeInfoByNoticeId(noticeId));
				noticeStaff.setStaffInfo(staffInfoService
						.queryByStaffId(newStaffId));
				noticeStaff.setStates(IDEDStates.ENABLE);
				staffDao.addNoticeStaff(noticeStaff);
			}
		}
		if (sectionIds.length()!= 0) {
			sectionIds = sectionIds.substring(0, sectionIds.length() - 1);
			String[] arraySectionId = sectionIds.split(",");
			for (int i = 0; i < arraySectionId.length; i++) {
				Integer newSectionId = Integer.parseInt(arraySectionId[i]);
				noticeSection
						.setNoticeInfo(queryNoticeInfoByNoticeId(noticeId));
				noticeSection.setSectionInfo(sectionInfoService
						.queryBySectionId(newSectionId));
				noticeSection.setStates(IDEDStates.ENABLE);
				sectionDao.addNoticeSection(noticeSection);
			}
		}
		return noticeId;
	}

	public String deleteNotice(String noticeIds) throws Exception {
		String unDelIds = "";
		String[] arrayNoticeId = noticeIds.split(",");
		for (String noticeIdStr : arrayNoticeId) {
			int noticeId = Integer.parseInt(noticeIdStr);
			noticeInfo = queryNoticeInfoByNoticeId(noticeId);
			noticeInfo.setStates(IDEDStates.DELETE);
			listNoticeStaff = queryStaffDetail(noticeId);
			for (int i = 0; i < listNoticeStaff.size(); i++) {
				noticeStaff = listNoticeStaff.get(i);
				noticeStaff.setStates(IDEDStates.DELETE);
				staffDao.deleteNoticeStaff(noticeStaff);
			}
			listNoticeSection = querySectionDetail(noticeId);
			for (int i = 0; i < listNoticeSection.size(); i++) {
				noticeSection = listNoticeSection.get(i);
				noticeSection.setStates(IDEDStates.DELETE);
				sectionDao.deleteNoticeSection(noticeSection);
			}
			boolean result = infoDao.deleteNoticeInfo(noticeInfo);
			if (result == false) {
				unDelIds = noticeId + ",";
				continue;
			}
		}
		return unDelIds;
	}

	public String deleteNotice(int noticeId) throws Exception {
		noticeInfo = queryNoticeInfoByNoticeId(noticeId);
		noticeInfo.setStates(-1);
		listNoticeStaff = queryStaffDetail(noticeId);
		for (int i = 0; i < listNoticeStaff.size(); i++) {
			noticeStaff = listNoticeStaff.get(i);
			noticeStaff.setStates(IDEDStates.DELETE);
			staffDao.deleteNoticeStaff(noticeStaff);
		}
		listNoticeSection = querySectionDetail(noticeId);
		for (int i = 0; i < listNoticeSection.size(); i++) {
			noticeSection = listNoticeSection.get(i);
			noticeSection.setStates(IDEDStates.DELETE);
			sectionDao.deleteNoticeSection(noticeSection);
		}
		infoDao.deleteNoticeInfo(noticeInfo);
		return " ";
	}

	public NoticeInfoValue queryNoticeInfoByNoticeId(int noticeId)
			throws Exception {

		return infoDao.queryNoticeInfoByNoticeId(noticeId);
	}

	public List<NoticeSectionValue> querySectionDetail(int noticeId)
			throws Exception {
		return sectionDao.queryNoticeSectionByNoticeId(noticeId);
	}

	public List<NoticeStaffValue> queryStaffDetail(int noticeId)
			throws Exception {
		return staffDao.queryNoticeStaffByNoticeId(noticeId);
	}

	public List<NoticeInfoValue> queryNoticeInfoWithPage(String place,
			String title, int rows, int pageNo) throws Exception {
		if (StringUtils.isNotEmpty(place) && StringUtils.isNotBlank(place)) {
			noticeInfo.setPlace(place);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			noticeInfo.setTitle("%"+title+"%");
		}
		noticeInfo.setStates(IDEDStates.ENABLE);
		return infoDao.queryNoticeInfoWithPage(noticeInfo, rows, pageNo);
	}

	public List<NoticeInfoValue> queryTipWithPage(int sectionId, int staffId,
			String title, String place, int rows, int pageNo) throws Exception {
		if (StringUtils.isNotEmpty(place) && StringUtils.isNotBlank(place)) {
			noticeInfo.setPlace(place);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			noticeInfo.setTitle("%"+title+"%");
		}
		noticeInfo.setStates(IDEDStates.ENABLE);
		return infoDao.queryTipWithPage(sectionId, staffId, noticeInfo, rows,
				pageNo);
	}

	public int countTpages(String place, String title, int rows)
			throws Exception {
		if (StringUtils.isNotEmpty(place) && StringUtils.isNotBlank(place)) {
			noticeInfo.setPlace(place);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			noticeInfo.setTitle("%"+title+"%");
		}
		noticeInfo.setStates(IDEDStates.ENABLE);
		return infoDao.countTpages(noticeInfo, rows);
	}

	public int countTpagesByTip(int sectionId, int staffId, String title,
			String place, int rows) throws Exception {
		if (StringUtils.isNotEmpty(place) && StringUtils.isNotBlank(place)) {
			noticeInfo.setPlace(place);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			noticeInfo.setTitle("%"+title+"%");
		}
		noticeInfo.setStates(IDEDStates.ENABLE);
		return infoDao.countTpagesByTip(sectionId, staffId, noticeInfo, rows);
	}

	public int countTpagesByStaffId(int staffId, String place, String title,
			int rows) throws Exception {
		if (StringUtils.isNotEmpty(place) && StringUtils.isNotBlank(place)) {
			noticeInfo.setPlace(place);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			noticeInfo.setTitle("%"+title+"%");
		}
		noticeInfo.setStates(IDEDStates.ENABLE);
		return infoDao.countTpagesByStaffId(staffId, noticeInfo, rows);
	}

	public List<NoticeInfoValue> queryNoticeInfoByStaffIdWithPage(int staffId,
			String place, String title, int rows, int pageNo) throws Exception {
		if (StringUtils.isNotEmpty(place) && StringUtils.isNotBlank(place)) {
			noticeInfo.setPlace(place);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			noticeInfo.setTitle("%"+title+"%");
		}
		noticeInfo.setStates(IDEDStates.ENABLE);
		return infoDao.queryNoticeInfoByStaffIdWithPage(staffId, noticeInfo,
				rows, pageNo);
	}

	public int getTotalNumber() throws Exception {
		return infoDao.getTotalNumber();
	}

	public List<NoticeInfoValue> queryTip() throws Exception {

		return infoDao.queryTip();
	}
	
	public INoticeInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(INoticeInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public INoticeSectionDao getSectionDao() {
		return sectionDao;
	}

	public void setSectionDao(INoticeSectionDao sectionDao) {
		this.sectionDao = sectionDao;
	}

	public INoticeStaffDao getStaffDao() {
		return staffDao;
	}

	public void setStaffDao(INoticeStaffDao staffDao) {
		this.staffDao = staffDao;
	}

	public NoticeInfoValue getNoticeInfo() {
		return noticeInfo;
	}

	public void setNoticeInfo(NoticeInfoValue noticeInfo) {
		this.noticeInfo = noticeInfo;
	}

	public NoticeSectionValue getNoticeSection() {
		return noticeSection;
	}

	public void setNoticeSection(NoticeSectionValue noticeSection) {
		this.noticeSection = noticeSection;
	}

	public NoticeStaffValue getNoticeStaff() {
		return noticeStaff;
	}

	public void setNoticeStaff(NoticeStaffValue noticeStaff) {
		this.noticeStaff = noticeStaff;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}

	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}

	public List<NoticeStaffValue> getListNoticeStaff() {
		return listNoticeStaff;
	}

	public void setListNoticeStaff(List<NoticeStaffValue> listNoticeStaff) {
		this.listNoticeStaff = listNoticeStaff;
	}

	public List<NoticeSectionValue> getListNoticeSection() {
		return listNoticeSection;
	}

	public void setListNoticeSection(List<NoticeSectionValue> listNoticeSection) {
		this.listNoticeSection = listNoticeSection;
	}

}
